# -*- coding: utf-8 -*-
class WaterMeterController < ApplicationController
  before_filter :user_authorize
  before_filter :sys_before_log
  
  def getWaterRecordsByOwnerId
    houseId = params[:house_id]
    waterMeters = WaterMeter.where("house_id = ?",houseId)
    jsonStr="{totalProperty:"+waterMeters.length.to_s+",root:"+waterMeters.to_json(:include=>[:user])+"}"
    puts jsonStr
    render :text=>jsonStr, :layout=>false
  end
  
  def getWaterMeters
    #根据单元，年，月，得到相关记录，并使用outter join
    buildingId = params[:building_id]
    year = params[:year].to_i
    month = params[:month].to_i
    #SELECT `houses`.* FROM `houses` right outer join water_meters ON houses.id = water_meters.house_id AND water_meters.current_year = 2009 AND water_meters.current_month = 9 AND houses.building_id = 2
    #houses = House.where(:all,:joins=>[" left outer join water_meters ON houses.id = water_meters.house_id AND water_meters.current_year = 2009 AND water_meters.current_month = 9 AND houses.building_id = 2"])
    #houses = House.where(:all,:joins[])
    #houses = House.find(:joins=> "LEFT JOIN water_meters ON houses.id = water_meters.house_id and water_meters.current_year =#{year} AND water_meters.current_month =#{month} AND houses.building_id = #{buildingId}",:conditions=>["building_id = ?",buildingId])
    #houses = House.where(:all,:conditions=>["building_id = ? ",buildingId])
    #    for house in houses
    #      house.water_meters.where(:all,:conditions=>["current_year = ? AND current_month = ?",year,month])
    #      puts "====================="
    #      puts house.water_meters.length
    #    end
    #houses = House.where(:all,:include=>"water_meters",:conditions=>["building_id = ? AND water_meters.current_year = ? AND water_meters.current_month = ? ",buildingId,year,month],:joins=>"LEFT OUTER JOIN water_meters ON houses.id = water_meters.id")
    #houses = House.where(:all,:include=>"water_meters",:conditions=>["building_id = ?",buildingId],:joins=>"LEFT OUTER JOIN water_meters ON houses.id = water_meters.id AND water_meters.current_year = #{year} AND water_meters.current_month = #{month}")
    #houses = House.where(:all,:conditions=>["building_id = ?",buildingId],:joins=>"LEFT OUTER JOIN water_meters ON houses.id = water_meters.id AND water_meters.current_year = #{year} AND water_meters.current_month = #{month}")
    #houses = House.where(:all,:conditions=>["building_id = ?",buildingId])
    houses = House.find_by_sql("select houses.id,houses.number,houses.owner,houses.phone,houses.water_last_meter,water_meters.current_meter,water_meters.id,water_meters.created_at,water_meters.updated_at as water_meter_id,water_meters.is_paid from houses LEFT OUTER JOIN water_meters on houses.id = water_meters.house_id AND water_meters.current_year = #{year} AND water_meters.current_month = #{month} WHERE houses.building_id = #{buildingId}")
    for house in houses
      #puts house.water_meters.length
      puts house.current_meter
    end
    #jsonStr="{totalProperty:"+houses.length.to_s+",root:"+houses.to_json(:include=>[:water_records])+"}"
    jsonStr="{totalProperty:"+houses.length.to_s+",root:"+houses.to_json()+"}"
    puts jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def saveOrUpdateWaterMeter
    puts params
    year = params[:year]
    month = params[:month]
    houseId = params[:house_id]
    waterMeterId = params[:water_meter_id].to_i
    meter = params[:meter]
    house = House.find(houseId)
    if waterMeterId == 0 #等于0就是从来有记录过。
      waterMeter = WaterMeter.new
      waterMeter.current_year = year
      waterMeter.current_month = month
      waterMeter.current_meter = meter
      house.water_meters << waterMeter
      house.save
    else
      waterMeter = WaterMeter.find(waterMeterId)
      waterMeter.current_meter = meter
      house.water_meters << waterMeter
      house.save
    end
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
end
